#include "AssistsettingView.h"
#include "ui_AssistsettingForm.h"
#include "SettingHelper.h"
#include <QValidator>
#include <QRegExp>
#include <QDebug>
#include "Auxiliary/View/MessageBoxDialog.h"


AssistsettingView::AssistsettingView(QWidget *parent) : QWidget(parent),
  ui(new Ui::Assistseting)
{
   ui->setupUi(this);

   QValidator *validator = new QIntValidator(0, 11, ui->nu_E_digit);
   ui->nu_E_digit->setValidator(validator);


   QRegExp regx1("[a-zA-Z0-9_-]{0,16}");
   QValidator *validator1 = new QRegExpValidator(regx1, ui->en_E_samplenumber );
   ui->en_E_samplenumber->setValidator( validator1 );

   SettingHelper *settingHelper = SettingHelper::getSettingHelper();


   ui->comboBox_sampleentering->setCurrentIndex(settingHelper->m_AssistSet.sampleNumEntryMode);
   ui->nu_E_digit->setText(QString::number(settingHelper->m_AssistSet.notAutoIncreaseDigit));
   ui->comboBox_nextsampleentering->setCurrentIndex(settingHelper->m_AssistSet.nextSampleEntryMode);

   ui->comboBox_startnum->setCurrentIndex(settingHelper->m_AssistSet.startDisplaySampleMode);
   ui->en_E_samplenumber->setText(QString::number(settingHelper->m_AssistSet.startDisplaySampleNum));
   ui->comboBox_model->setCurrentIndex(settingHelper->m_AssistSet.startDisplayTestMode);

   ui->radioButton_open->setChecked(settingHelper->m_AssistSet.preDilReminderEnable);
   ui->radioButton_close->setChecked((settingHelper->m_AssistSet.preDilReminderEnable == true)? false:true);
   ui->radioButton_open_2->setChecked(settingHelper->m_AssistSet.softKeyboardEnable);
   ui->radioButton_close_2->setChecked((settingHelper->m_AssistSet.softKeyboardEnable == true)? false:true);
   ui->comboBox_questionable->setCurrentIndex(settingHelper->m_AssistSet.alarmFlagR);
   ui->comboBox_hight->setCurrentIndex(settingHelper->m_AssistSet.alarmFlagH);
   ui->comboBox_low->setCurrentIndex(settingHelper->m_AssistSet.alarmFlagL);

   // 设置单选按钮的样式
   QString radiobuttonstyle = "QRadioButton::indicator {width: 13px;height: 13px;}";
   ui->radioButton_open->setStyleSheet(radiobuttonstyle);
   ui->radioButton_open_2->setStyleSheet(radiobuttonstyle);

   ui->radioButton_close->setStyleSheet(radiobuttonstyle);
   ui->radioButton_close_2->setStyleSheet(radiobuttonstyle);

   ui->groupBox->setFocus();
   logtemp.clear();  // 操作设置记录初始化

}

AssistsettingView::~AssistsettingView()
{
    delete ui;
}

void AssistsettingView::closeEvent(QCloseEvent *event)
{
    if(canSaveSettings() == false)
    {
         event->ignore();
        return;
    }

    if(isSettingModified() == true)
    {
        MessageBoxDialog *messageBox = new MessageBoxDialog(this,\
                                 QString(tr("是否保存？")));
        int result = messageBox->exec();
        if(result == QDialog::Accepted)
        {
            saveSettings();
            if(m_WarnFlagModifed == true)
            {
               // emit warnFlagChanged();   //发送报警标记改变信息
            }

            if(m_PreDilModifed == true)
            {
                // 预稀释提醒开关改变
            }

        }
    }
}

void AssistsettingView::on_comboBox_sampleentering_currentIndexChanged(int index)
{
    if(index == Manual)
    {
       ui->nu_E_digit->setEnabled(false);
    }
    else if(index == Auto)
    {
       ui->nu_E_digit->setEnabled(true);
    }
}

void AssistsettingView::on_comboBox_startnum_currentIndexChanged(int index)
{
   if(index == CustomSpNum)
   {
       ui->en_E_samplenumber->setEnabled(true);
       ui->comboBox_model->setEnabled(true);
   }
   else if(index == OldSpNum)
   {
       ui->en_E_samplenumber->setEnabled(false);
       ui->comboBox_model->setEnabled(false);
   }
}

bool AssistsettingView::canSaveSettings()
{
    if(ui->comboBox_startnum->currentIndex() == CustomSpNum) // 自定义
    {
        QString arg1 = ui->en_E_samplenumber->text();
        int length = arg1.length();
        bool ok;
        int num = arg1.mid(length-1,1).toInt(&ok,10);
        if(!ok)
        {

            MessageBoxDialog *messageBox = new MessageBoxDialog(this,\
                                     QString(tr("样本编号结束字符必须为数字!")));

            int result = messageBox->exec();

            if(result == QDialog::Accepted)
            {
                return false;
            }
            else
            {
                return false;
            }
        }

    }

    return true;
}

void AssistsettingView::saveSettings()
{
    SettingHelper *settingHelper = SettingHelper::getSettingHelper();

    settingHelper->m_AssistSet.sampleNumEntryMode = ui->comboBox_sampleentering->currentIndex();
    settingHelper->m_AssistSet.notAutoIncreaseDigit = ui->nu_E_digit->text().toInt();
    settingHelper->m_AssistSet.nextSampleEntryMode = ui->comboBox_nextsampleentering->currentIndex();

    settingHelper->m_AssistSet.startDisplaySampleMode = ui->comboBox_startnum->currentIndex();
    settingHelper->m_AssistSet.startDisplaySampleNum = ui->en_E_samplenumber->text().toDouble();
    settingHelper->m_AssistSet.startDisplayTestMode = ui->comboBox_model->currentIndex();

    settingHelper->m_AssistSet.preDilReminderEnable = ui->radioButton_open->isChecked();
    settingHelper->m_AssistSet.softKeyboardEnable = ui->radioButton_open_2->isChecked();
    settingHelper->m_AssistSet.alarmFlagR =  ui->comboBox_questionable->currentIndex();
    settingHelper->m_AssistSet.alarmFlagH = ui->comboBox_hight->currentIndex();
    settingHelper->m_AssistSet.alarmFlagL = ui->comboBox_low->currentIndex();

    settingHelper->assistWriteSetting();  // 控件设置存到Helper后，同时写到配置文件中
}

bool AssistsettingView::isSettingModified()
{
    bool ret = false;
    QString strBefore, strAfter;
    m_WarnFlagModifed = false;
    m_PreDilModifed = false;

    if(SettingHelper::getSettingHelper()->m_AssistSet.sampleNumEntryMode != ui->comboBox_sampleentering->currentIndex())
    {
        strBefore = ui->comboBox_sampleentering->itemText(SettingHelper::getSettingHelper()->m_AssistSet.sampleNumEntryMode);
        strAfter = ui->comboBox_sampleentering->currentText();

        logtemp.append(tr("修改辅助设置：编号录入方式：")+strBefore+tr("→")+strAfter);
        ret = true;
    }

    if(SettingHelper::getSettingHelper()->m_AssistSet.notAutoIncreaseDigit != ui->nu_E_digit->text().toInt())
    {
        strBefore = QString::number(SettingHelper::getSettingHelper()->m_AssistSet.notAutoIncreaseDigit);
        strAfter = ui->nu_E_digit->text();

        logtemp.append(tr("修改辅助设置：不参与自动递增：")+strBefore+tr("→")+strAfter);
        ret = true;
    }

    if(SettingHelper::getSettingHelper()->m_AssistSet.nextSampleEntryMode != ui->comboBox_nextsampleentering->currentIndex())
    {
       strBefore = ui->comboBox_nextsampleentering->itemText(SettingHelper::getSettingHelper()->m_AssistSet.nextSampleEntryMode);
       strAfter = ui->comboBox_nextsampleentering->currentText();

       logtemp.append(tr("修改辅助设置：下一样本录入方式：")+strBefore+tr("→")+strAfter);
       ret = true;
    }

    if(SettingHelper::getSettingHelper()->m_AssistSet.startDisplaySampleMode != ui->comboBox_startnum->currentIndex())
    {
        strBefore = ui->comboBox_startnum->itemText(SettingHelper::getSettingHelper()->m_AssistSet.startDisplaySampleMode);
        strAfter = ui->comboBox_startnum->currentText();

        logtemp.append(tr("修改辅助设置：开机起始样本：")+strBefore+tr("→")+strAfter);
        ret = true;
    }

    if(SettingHelper::getSettingHelper()->m_AssistSet.startDisplaySampleNum != ui->en_E_samplenumber->text().toLongLong())
    {
        strBefore = QString::number(SettingHelper::getSettingHelper()->m_AssistSet.startDisplaySampleNum);
        strAfter = ui->en_E_samplenumber->text();

        logtemp.append(tr("修改辅助设置：样本编号：")+strBefore+tr("→")+strAfter);
        ret = true;
    }

    if(SettingHelper::getSettingHelper()->m_AssistSet.startDisplayTestMode != ui->comboBox_model->currentIndex())
    {
        strBefore = ui->comboBox_model->itemText(SettingHelper::getSettingHelper()->m_AssistSet.startDisplayTestMode);
        strAfter = ui->comboBox_model->currentText();

        logtemp.append(tr("修改辅助设置：模式：")+strBefore+tr("→")+strAfter);
        ret = true;
    }

    if(SettingHelper::getSettingHelper()->m_AssistSet.preDilReminderEnable != ui->radioButton_open->isChecked())
    {
        if(ui->radioButton_open->isChecked())
            logtemp.append(tr("修改辅助设置：预稀释分析前提醒：关→开"));
           else
            logtemp.append(tr("修改辅助设置：预稀释分析前提醒：开→关"));

        m_PreDilModifed= true;
        ret = true;
    }

    if(SettingHelper::getSettingHelper()->m_AssistSet.softKeyboardEnable != ui->radioButton_open_2->isChecked())
    {
        if(ui->radioButton_open_2->isChecked())
         logtemp.append(tr("修改辅助设置：软键盘：关→开"));
        else
         logtemp.append(tr("修改辅助设置：软键盘：开→关"));
        ret = true;
    }

    if(SettingHelper::getSettingHelper()->m_AssistSet.alarmFlagR !=  ui->comboBox_questionable->currentIndex())
    {
        strBefore = ui->comboBox_questionable->itemText(SettingHelper::getSettingHelper()->m_AssistSet.alarmFlagR);
        strAfter = ui->comboBox_questionable->currentText();

        logtemp.append(tr("修改辅助设置：报警标记：可疑：")+strBefore+tr("→")+strAfter);
        m_WarnFlagModifed = true;
        ret = true;

    }

    if(SettingHelper::getSettingHelper()->m_AssistSet.alarmFlagH != ui->comboBox_hight->currentIndex())
    {
        strBefore = ui->comboBox_hight->itemText(SettingHelper::getSettingHelper()->m_AssistSet.alarmFlagH);
        strAfter = ui->comboBox_hight->currentText();

        logtemp.append(tr("修改辅助设置：报警标记：高：")+strBefore+tr("→")+strAfter);
        m_WarnFlagModifed = true;
        ret = true;
    }

    if(SettingHelper::getSettingHelper()->m_AssistSet.alarmFlagL != ui->comboBox_low->currentIndex())
    {
        strBefore = ui->comboBox_low->itemText(SettingHelper::getSettingHelper()->m_AssistSet.alarmFlagL);
        strAfter =  ui->comboBox_low->currentText();

        logtemp.append(tr("修改辅助设置：报警标记：低：")+strBefore+tr("→")+strAfter);
        m_WarnFlagModifed = true;
        ret = true;
    }

    qDebug()<<"logtemp"<<logtemp;

    return ret;
}
